package net.chery.rm;

import lombok.Data;
import net.chery.entity.VehProduce;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;

import static net.chery.Cons.VIN_LENGTH;

/**
 * @author gai
 */
public abstract class VinRowMapper<T> implements RowMapper<T> {

    protected static final Logger logger = LoggerFactory.getLogger(VinRowMapper.class);

    protected final Set<String> vehModels;
    private final Function<String, T> function;
    protected final Consumer<VehProduce> consumer;

    public VinRowMapper(Set<String> vehModels, Consumer<VehProduce> consumer, Function<String, T> function) {
        this.vehModels = vehModels;
        this.function = function;
        this.consumer = consumer;
    }

    protected T getNew(String vin){
        return function.apply(vin);
    }

    protected boolean checkData(String vin, String vehModel) {
        if (!checkVin(vin)) {
            logger.debug("error vin:{}", vin);
            return false;
        }
        if (!StringUtils.hasLength(vehModel) || !vehModels.contains(vehModel)) {
            logger.debug("is not battery:{}", vin);
            return false;
        }
        return true;
    }

    public static Date parseDate(String date, SimpleDateFormat sdf){
        if(date!=null){
            try {
                return sdf.parse(date);
            } catch (ParseException e) {
                logger.error("error date:{},format:{}",date,sdf.toPattern());
            }
        }
        return null;
    }

    /**
     * 检验vin码
     */
    public static boolean checkVin(String vin) {
        // 使用ascii码运算 '0'为48，'Z'为90
        if (vin == null || vin.length() != VIN_LENGTH) {
            return false;
        }
        // 对应数值，加权系数，校验位，对应数值*加权系数的和
        int symbol, weight, verify, sum = 0;
        boolean isRepeat = true;
        // 数组从0开始,下标0...16,0为第一位，16为第17位
        char[] vinCharArr = vin.toCharArray();
        /* 第九位（校验位）为0...9加 X */
        if (vinCharArr[8] >= 48 && vinCharArr[8] <= 57) {
            //0到9
            verify = vinCharArr[8] - 48;
        } else if (vinCharArr[8] == 88) {
            //X
            verify = 10;
        } else {
            return false;
        }

        for (int i = 0; i < vinCharArr.length; i++) {
            // 给VIN码中任意一位指定加权系数 第一位-8 第二位-7...第七位-2 第八位-10 | 第九位为校验位 第十位-9 第十一位-8...第十七位-2
            if (i < 7) {
                weight = 8 - i;
            } else if (i > 8) {
                weight = 18 - i;
            } else if (i == 7) {
                weight = 10;
            } else {
                continue;
            }
            // 每位的对应数值 0-0 1-1 2-2...9-9 A-1 B-2 C-3...H-8 J-1 K-2 L-3...N-5 P-7 R-9 S-2 T-3..Z-9 （无I、O、Q）
            if (vinCharArr[i] >= 48 && vinCharArr[i] <= 57) {
                //0-9
                symbol = vinCharArr[i] - 48;
            } else if (vinCharArr[i] >= 65 && vinCharArr[i] <= 72) {
                //A-H
                symbol = vinCharArr[i] - 64;
            } else if (vinCharArr[i] >= 74 && vinCharArr[i] <= 82 && vinCharArr[i] != 79 && vinCharArr[i] != 81) {
                //J-R不含O,Q
                symbol = vinCharArr[i] - 73;
            } else if (vinCharArr[i] >= 83 && vinCharArr[i] <= 90) {
                //S-Z
                symbol = vinCharArr[i] - 81;
            } else {
                return false;
            }
            sum += symbol * weight;
            if (isRepeat && i > 0) {
                // SSSSSSSS校验位SSSSSSSS 时 isRepeat=true
                isRepeat = vinCharArr[i] == vinCharArr[i - (i != 9 ? 1 : 2)];
            }
        }
        if (isRepeat) {
            return false;
        }
        return verify == sum % 11;
    }
}
